package com.aek.aekdroid;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.aek.aekdroid.data.IDbConstants;

public class AddDetailsActivity extends BaseActivity {
	public static final String TAG = "AddDetailsActivity";
	private TextView title;
	private TextView match;
	private Spinner type;
	private TextView minute;
	private TextView score;
	private TextView player;
	private Spinner side;
	private Button ok;
	private Button cancel;
	private int matchId = -1;
	private int id = -1;
	private String matchFullTitle = "";
	private ArrayAdapter<CharSequence> typeAdapter;
	private ArrayAdapter<CharSequence> sideAdapter;
	public static final String DETAIL_ID = "DETAIL_ID";
	public static final String MATCH_ID = "MATCH_ID";
	public static final String MATCH = "MATCH";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_details);
		title = (TextView) findViewById(R.id.add_details_title);
		match = (TextView) findViewById(R.id.add_details_matchFullTitle);
		type = (Spinner) findViewById(R.id.add_details_type);
		side = (Spinner) findViewById(R.id.add_details_side);
		minute = (TextView) findViewById(R.id.add_details_minute);
		score = (TextView) findViewById(R.id.add_details_score);
		player = (TextView) findViewById(R.id.add_details_player);
		ok = (Button) findViewById(R.id.add_details_ok);
		cancel = (Button) findViewById(R.id.add_details_cancel);
		cancel.setOnClickListener(new ClickListener());
		ok.setOnClickListener(new ClickListener());
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			matchId = bundle.getInt(MATCH_ID, -1);
			id = bundle.getInt(DETAIL_ID, -1);
			matchFullTitle = aek.getMatchData().getMatchFullTitle(matchId);
		}
		if (id > -1) {
			title.setText(R.string.add_details_editDetails);
			ok.setText(R.string.add_details_edit);
		}
		typeAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, IDbConstants.TYPES);
		typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sideAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, IDbConstants.SIDES);
		sideAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		loadMatchDetail();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	public void onBackPressed() {
		Intent showMatchIntent = new Intent(this, ShowMatchActivity.class);
		showMatchIntent.putExtra(IDbConstants.MATCHES_C_ID, matchId);
		startActivity(showMatchIntent);
	}

	public void loadMatchDetail() {
		type.setAdapter(typeAdapter);
		side.setAdapter(sideAdapter);
		match.setText(matchFullTitle);
		if (id > -1) {
			Cursor cursor = aek.getMatchData().getMatchDetail(id);
			if (cursor.moveToFirst()) {
				minute.setText(cursor.getString(cursor.getColumnIndex(IDbConstants.DETAILS_C_MIN)));
				player.setText(cursor.getString(cursor.getColumnIndex(IDbConstants.DETAILS_C_PLAYER)));
				score.setText(cursor.getString(cursor.getColumnIndex(IDbConstants.DETAILS_C_COMMENT)));
				type.setSelection(cursor.getInt(cursor.getColumnIndex(IDbConstants.DETAILS_C_TYPE)));
				side.setSelection(cursor.getInt(cursor.getColumnIndex(IDbConstants.DETAILS_C_SIDE)));
			}
			cursor.close();
		}

	}

	public void submit() {
		String pass = aek.getPassword();
		String strPlayer = player.getText().toString();
		int intType = type.getSelectedItemPosition();
		int intSide = side.getSelectedItemPosition();
		String strMinute = minute.getText().toString();
		String strScore = score.getText().toString();

		String query = "&pass=" + pass + "&id=" + this.id + "&match_id=" + this.matchId + "&type=" + intType + "&side=" + intSide
				+ "&player=" + Uri.encode(strPlayer) + "&minute=" + Uri.encode(strMinute) + "&score=" + Uri.encode(strScore);

		String url = WebAekDroid.ADD_DETAIL_URL + query;
		MyLog.d(TAG, "Submiting detail " + url);
		Helper.adminFunction(this, url);
	}

	class ClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.add_details_ok) {
				AddDetailsActivity.this.submit();
			} else if (v.getId() == R.id.add_details_cancel) {
				finish();
			}

		}

	}

}
